/*
 * Copyright 2008-2023 dexian.vip. All rights reserved.
 * Support: http://www.dexian.vip
 * License: http://www.dexian.vip/license
 */

package vip.dexian.admin.controller;

import vip.dexian.admin.base.BaseController;
import vip.dexian.admin.service.FileService;
import vip.dexian.common.Message;
import vip.dexian.core.enums.FileTypeEnum;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传controller
 *
 * @author 挺好的 2023年06月01日 16:24
 */
@Controller ("adminUploadController")
@RequestMapping ("/admin/upload")
public class UploadController extends BaseController {

    /**
     * 文件上传
     */
    @Resource (name = "fileServiceImpl")
    private FileService fileService;

    /**
     * 上传文件
     *
     * @param fileType
     *         文件类型
     * @param file
     *         待上传的文件
     *
     * @return {@link Message}
     */
    @PostMapping
    @ResponseBody
    public Message upload (FileTypeEnum fileType, MultipartFile file) {

        // 校验文件类型
        this.fileService.valid(fileType, file);

        String url = this.fileService.upload(fileType, file, false);
        if (StringUtils.isEmpty(url)) {
            return Message.error("00007");
        }

        return Message.success(url);
    }
}
